
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;

var nsSuperfishComponentConstants = {
	// Superfish
//	extensionName: "superfish.com"
	
	// IeTab
	extensionName: "ip.cn"
}

var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);

//function chrome_alert(title, message) 
//{
//    try{
//        var ps = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
//        ps.alert(null, title, message);
//    }
//    catch(ex){
//    }
//}
var sfLogger = {
		write: function(string){
//		    var gConsole = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
//		    dump("*** " + string + "\n");
//		    if (gConsole) {
//		      gConsole.logStringMessage(string);
//		    }
		}
}


/* ctor
 */
function nsSuperfishComponent() {
    // Initialize data properties.
	sfLogger.write("nsSuperfishComponent()");
}

nsSuperfishComponent.prototype = {
    aProgressListener: null,
    
    // ---------- nsIObserver methods ----------
    observe: function( anObject, aTopic, aData ) {
        
		sfLogger.write("observe() aTopic="+aTopic);
        switch ( aTopic ) {
        case "app-startup":
			sfLogger.write("observe 1 ");
            
            this.aProgressListener = Cc["@" + nsSuperfishComponentConstants.extensionName + "/superfishprogresslistener;1"].createInstance(Ci.nsIWebProgressListener);
            var aWebProgress = Cc["@mozilla.org/docloaderservice;1"].getService(Ci.nsIWebProgress);
            aWebProgress.addProgressListener(this.aProgressListener, Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);

			sfLogger.write("observe 2 ");
           
            break;
        case "onload":
            break;
        case "oncancel":
            break;
        case "onpause":
            break;
        case "onlaunch":
            break;
        case "onreveal":
            break;
        case "onunload":
            break;
        case "oncompleted":
            break;
        default:
            break;
        }
    },

    // ---------- nsISupports methods ----------

    QueryInterface: function (iid) {
        
        if (
            iid.equals(Components.interfaces.nsIObserver) ||
            iid.equals(Components.interfaces.nsIInterfaceRequestor) ||
            iid.equals(Components.interfaces.nsISupports))
        {
            return this;
        }
        Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
        return null;
    },

    // ---------- nsIInterfaceRequestor methods ----------

    getInterface: function(iid) {
        
        Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
        return null;
    },

    // ---------- implementation methods ----------
}

// This Component's module implementation.  All the code below is used to get this
// component registered and accessible via XPCOM.
var module = {
    // registerSelf: Register this component.
    registerSelf: function (compMgr, fileSpec, location, type) {
    
        var compReg = compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
        compReg.registerFactoryLocation( this.cid,
                                         "superfish extension",
                                         this.contractId,
                                         fileSpec,
                                         location,
                                         type );
                                         
        categoryManager.addCategoryEntry("app-startup", "superfish extension","service," + this.contractId, true, true);
            
    },

    // getClassObject: Return this component's factory object.
    getClassObject: function (compMgr, cid, iid) {
        if (!cid.equals(this.cid))
            throw Components.results.NS_ERROR_NO_INTERFACE;

        if (!iid.equals(Components.interfaces.nsIFactory))
            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

        return this.factory;
    },

    /* CID for this class */
    cid: Components.ID("{3B23EEC0-2CB7-4e25-B67A-3881931607F1}"),

    /* Contract ID for this class */
    contractId: "@" + nsSuperfishComponentConstants.extensionName + "/superfishextension;1",

    /* factory object */
    factory: {
        // createInstance: Return a new nsSuperfishComponent object.
        createInstance: function (outer, iid) {
            if (outer != null)
                throw Components.results.NS_ERROR_NO_AGGREGATION;

            return (new nsSuperfishComponent()).QueryInterface(iid);
        }
    },

    // canUnload: n/a (returns true)
    canUnload: function(compMgr) {
        return true;
    }
};

// NSGetModule: Return the nsIModule object.
function NSGetModule(compMgr, fileSpec) {

	sfLogger.write("nsSuperfishComponent::NSGetModule()", module);
    return module;
}